package com.umerclass.ui.anmo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.umerclass.AppDroid;
import com.umerclass.R;
import com.umerclass.bean.anmo.UserOtherBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.model.MsgBean;
import com.umerclass.frame.okhttp.OkEntityRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.ui.anmo.three.DianJiaActivity;
import com.umerclass.utils.Constants;
import com.umerclass.utils.EventConstants;
import butterknife.BindView;
import butterknife.OnClick;

public class AnMoLoginActivity extends BaseActivity {

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.linear_register)
    LinearLayout linearRegister;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.linear)
    LinearLayout linear;
    private int type = 1;//1服务商合伙人登录  2店家登录
    private String phone = "";
    private String password = "";
    private boolean click = false;
    private UserOtherBean userOtherBean;
    private boolean showPassword = false;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_anmo_login;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));
        etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                phone = s.toString();
                setOne();
            }
        });

        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                password = s.toString();
                setOne();
            }
        });
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.tv1, R.id.tv2, R.id.tv_login, R.id.linear_back,
            R.id.tv_forget_password, R.id.linear_register,R.id.linear})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv1:
                if (type == 1) {
                    return;
                } else {
                    type = 1;
                    tv1.setBackgroundResource(R.drawable.shape_one_press);
                    tv2.setBackgroundResource(R.drawable.shape_one_normal);
                    tv1.setTextColor(Color.parseColor("#ffffff"));
                    tv2.setTextColor(Color.parseColor("#212121"));
                    setEmpty();
                    setOne();
                    showPassword = false;
                    image.setBackgroundResource(R.drawable.suo_press);
                }
                break;
            case R.id.tv2:
                if (type == 2) {
                    return;
                } else {
                    type = 2;
                    tv1.setBackgroundResource(R.drawable.shape_one_normal);
                    tv2.setBackgroundResource(R.drawable.shape_one_press);
                    tv1.setTextColor(Color.parseColor("#212121"));
                    tv2.setTextColor(Color.parseColor("#ffffff"));
                    setEmpty();
                    setOne();
                    showPassword = false;
                    image.setBackgroundResource(R.drawable.suo_press);
                }
                break;
            case R.id.tv_login:
                if (click) {
                    showProgress("正在登录...");
                    OkEntityRequest<UserOtherBean> request = new OkEntityRequest<>(R.id.userLogin,
                            Constants.userLogin, UserOtherBean.class);
                    request.addParams("phone", phone);
                    request.addParams("loginPassword", password);
                    if (type == 1) {
                        request.addHeadParams("userType", 1);
                    } else if (type == 2) {
                        request.addHeadParams("userType", 2);
                    }
                   /* request.addParams("authCode", businessType);
                    request.addParams("jsCode",10);*/
                    requestOkhttpEntity(request);
                }
                //switchTo(this, MyTeamActivity.class,true);
                //switchTo(this, DaiLiActivity.class,true);
                //switchTo(this, DianJiaActivity.class, true);
                break;
            case R.id.linear_register:
                switchTo(this, JoinActivity.class, false);
                break;
            case R.id.tv_forget_password:
                Intent intent = new Intent(AnMoLoginActivity.this,FindPasswordActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
            case R.id.linear:
                if (showPassword){
                    showPassword = false;
                    image.setBackgroundResource(R.drawable.suo_press);
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etPassword.setSelection(etPassword.getText().toString().length());
                }else {
                    showPassword = true;
                    image.setBackgroundResource(R.drawable.suo_normal);
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etPassword.setSelection(etPassword.getText().toString().length());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        if (msgBean.getFlag() == EventConstants.registerSuccess) {
            phone = (String) msgBean.getObject();
            etPhone.setText(phone);
            setOne();
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.userLogin:
                hideProgress();
                userOtherBean = (UserOtherBean) t.getT();
                userOtherBean.setPassword(password);
                userOtherBean.setPhone(phone);
                AppDroid.getInstance().setUserOtherBean(userOtherBean);
                if(type == 1){
                    int businessType = userOtherBean.getBusinessType();
                    Intent intent = new Intent(AnMoLoginActivity.this, DaiLiActivity.class);
                    intent.putExtra("businessType", businessType);
                    startActivity(intent);
                    finish();
                }else {
                    int businessType = userOtherBean.getBusinessType();
                    Intent intent = new Intent(AnMoLoginActivity.this, DianJiaActivity.class);
                    intent.putExtra("businessType", businessType);
                    startActivity(intent);
                    finish();
                }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.userLogin:
                hideProgress();
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    private void setOne() {
        if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)) {
            tvLogin.setBackgroundResource(R.drawable.shape33);
            click = true;
        } else {
            tvLogin.setBackgroundResource(R.drawable.shape3);
            click = false;
        }
    }

    private void setEmpty() {
        phone = "";
        password = "";
        etPhone.setText("");
        etPassword.setText("");
    }
}
